<?php
if (isset($_POST['editer'])) 
{
	$req = $bdd->prepare('SELECT * FROM images WHERE id = :id');
	$req->execute(array('id' => $_POST['id']));
	$donnees = $req->fetch();
	?>
	<h1>Editer une image</h1>
	<form method="post" action="index.php?page=admin_images" enctype="multipart/form-data">
		<table border="0" width="100%">
			<tr>
			<td rowspan="3" width="150"><img src="<?php echo $donnees['image']; ?>" width ="150" class="img-gauche"></td>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="50" required></td></tr>
			<td><label for="galerie">Galerie</label></td>
			<td><select name="galerie" id="galerie">
				<?php
				$req_cat = $bdd->query('SELECT * FROM galeries');
				while ($donnees_cat = $req_cat->fetch())
				{
					?>
					<option value="<?php echo $donnees_cat['id']; ?>" <?php if ($donnees_cat['id']==$donnees['galerie']) { echo 'selected'; }  ?>>
					<?php echo stripslashes($donnees_cat['nom']); ?>
					</option>
					<?php
				}
				?>
			</select></td></tr>
			<tr><td><label for="image">Image</label></td><td colspan="2"><input type="file" size="150" name="image" /></td></tr>
			<tr><td colspan="3"><label for="description">Description</label></td></tr>
			<tr><td colspan="3"><textarea id="description" name="description" rows="15" cols="80"><?php echo stripslashes($donnees['description']); ?></textarea></td></tr>
			<tr><td colspan="3"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Mettre à jour" name="modifier" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE images SET nom=:nom, galerie=:galerie, description=:description WHERE id=:id');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'galerie'  => $_POST['galerie'],
		'description' => stripslashes($_POST['description']),
		'id' => $_POST['id']
	));
	$nom_galerie = $_POST['galerie'];
	echo $nom_galerie;
	if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
	{
		if ($_FILES['image']['size'] <= 1000000)
		{
			$infosfichier = pathinfo($_FILES['image']['name']);
			$extension_upload = $infosfichier['extension'];
			$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
			if (in_array($extension_upload, $extensions_autorisees))
			{
				move_uploaded_file($_FILES['image']['tmp_name'], 'galeries/'.$nom_galerie.'/'.basename($_FILES['image']['name']));
				$chemin = 'galeries/'.$nom_galerie.'/'.basename($_FILES['image']['name']);
				$req = $bdd->prepare('UPDATE images SET image=:image WHERE id=:id');
				$req->execute(array(
					'image' => $chemin,
					'id' => $_POST['id']
				));
			}
		}
	}
	header('Location: index.php?page=admin_images'); 
}
elseif (isset($_POST['effacer'])) 
{
	$fichier=$_POST['image'];
	//echo $fichier;
	if (!unlink($fichier)) {
		die('Echec lors de la destruction de l\'image (fichier).');
	}
	else
	{
		$req = $bdd->prepare('DELETE FROM images WHERE id=:id');
		$req->execute(array('id' => $_POST['id']));
		header('Location: index.php?page=admin_images'); 
	}
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->query("SELECT MAX(id) AS nom_image FROM images");
	$donnees = $req->fetch();
	$nom_image=$donnees['nom_image']+1;
	$req = $bdd->query("SELECT MAX(id) AS nom_image FROM images");
	$donnees = $req->fetch();
	$nom_galerie=$_POST['galerie'];
	echo $nom_galerie.'/'.$nom_image;
	if (isset($_FILES['image']) AND $_FILES['image']['error'] == 0)
	{
		if ($_FILES['image']['size'] <= 1000000)
		{
			$infosfichier = pathinfo($_FILES['image']['name']);
			$extension_upload = $infosfichier['extension'];
			$extensions_autorisees = array('jpg', 'jpeg', 'gif', 'png');
			if (in_array($extension_upload, $extensions_autorisees))
			{
				move_uploaded_file($_FILES['image']['tmp_name'], 'galeries/'.$nom_galerie.'/'.$nom_image.'.'.$extension_upload);
				$chemin = 'galeries/'.$nom_galerie.'/'.$nom_image.'.'.$extension_upload;
				$req = $bdd->prepare('INSERT INTO images (nom, galerie, description, image) VALUES (:nom, :galerie, :description, :image)');
				$req->execute(array(
					'nom' => $_POST['nom'],
					'galerie'  => $_POST['galerie'],
					'description' => $_POST['description'],
					'image' => $chemin
				));
			}
		}
	}
	header('Location: index.php?page=admin_images');
}
elseif (isset($_POST['nouveau'])) 
{
	?>
   <h1>Ajouter une image</h1>
   <form method="post" action="index.php?page=admin_images" enctype="multipart/form-data">
		<table border="0" width="100%">
			<tr>
			<td><label for="nom">Nom</label></td>
			<td><input type="text" name="nom" value="" maxlength="250" size="50" required></td></tr>
			<td><label for="galerie">Galerie</label></td>
			<td><select name="galerie" id="galerie">
			<?php
			$req_cat = $bdd->query('SELECT * FROM galeries ORDER BY nom');
			while ($donnees_cat = $req_cat->fetch())
			{
				?>
				<option value="<?php echo $donnees_cat['id']; ?>">
				<?php echo stripslashes($donnees_cat['nom']); ?>
				</option>
				<?php
			}
			?>
			</select></td></tr>
			<tr><td colspan="2"><hr/></td></tr>
			<tr><td><label for="image">Image</label></td><td><input type="file" size="150" name="image" /></td></tr>
			<tr><td colspan="2"><hr/></td></tr>
			<tr><td colspan="2"><label for="description">Description</label></td></tr>
			<tr><td colspan="2"><textarea id="description" name="description" rows="15" cols="80"></textarea></td></tr>
			<tr><td colspan="2"><input type="submit" value="Ajouter cette nouvelle image" name="ajouter" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
else
{
	?>
	<form method="post" action="index.php?page=admin_images">
		<input type="submit" value="Ajouter une nouvelle image" name="nouveau" class="bouton" />
	</form>
	<?php
	$req_cat = $bdd->query('SELECT * FROM galeries ORDER BY nom');
	while ($donnees_cat = $req_cat->fetch())
	{
		?>
		<h1><?php echo $donnees_cat['nom']; ?></h1>
		<p><?php echo stripslashes($donnees_cat['description']); ?></p>
		<?php
		$req = $bdd->prepare('SELECT * FROM images WHERE galerie=:galerie ORDER BY nom');
		$req->execute(array('galerie' => $donnees_cat['id']));
		?>
		<table border="0" width="100%">
		<?php
		
		while ($donnees = $req->fetch())
		{
			?>
			<form method="post" action="index.php?page=admin_images">
				<tr>
				<td width="100"><img src="<?php echo $donnees['image']; ?>" width="100" class="img-gauche"></td>
				<td width="450"><b><?php echo stripslashes($donnees['nom']); ?></b><br/>
				<?php echo stripslashes($donnees['description']); ?></td>
				<td><input type="hidden" name="nom_galerie" value="<?php echo $donnees_cat['id']; ?>" />
				<input type="hidden" name="id" 			value="<?php echo $donnees['id']; ?>" />
				<input type="hidden" name="image" 		value="<?php echo $donnees['image']; ?>" />
				<input type="submit" name="editer"		value="Editer"  	class="bouton" />
				<input type="submit" name="effacer"		value="Effacer"  	class="bouton" /></td>
				</tr>
			</form>
			<?php 
		}
		?>
		</table>
		<?php
		$req->closeCursor();
	}
	$req_cat->closeCursor();
}